package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.*;
import cn.gson.boot.model.pojos.*;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class WeiXiuGongDanService {
    @Autowired
    WeiXiuGongDanDao weiXiuGongDanDao;
    @Autowired
    WeiXiuGongDanMapper weiXiuGongDanMapper;

    @Autowired
    WxJianCeMapper wxJianCeMapper;
    @Autowired
    WxJianCeDao wxJianCeDao;
    @Autowired
    WxQueDingMapper wxQueDingMapper;
    @Autowired
    WxQueDingDao wxQueDingDao;

    @Autowired
    WxGuoChengMapper wxGuoChengMapper;
    @Autowired
    WxGuoChengDao wxGuoChengDao;

    @Autowired
    WxJiaoFuMapper wxJiaoFuMapper;
    @Autowired
    WxJiaoFuDao wxJiaoFuDao;

    @Autowired
    WxShouKuanMapper wxShouKuanMapper;
    @Autowired
    WxShouKuanDao wxShouKuanDao;


    @Autowired
    WxgdXiangQingDao wxgdXiangQingDao;

    //根据客户查看联系人
    public List<Contacts> cklianxiren(Integer kId) {
        return weiXiuGongDanMapper.cklianxiren(kId);
    }

    //查看所有部门
    public List<Dept> ckAllBuMen() {
        return weiXiuGongDanMapper.ckAllBuMen();
    }

    //新建维修工单
    public void addWeiXiuGongDan(Weixiugongdan weixiugongdan) {
        weixiugongdan.setGdYs(1);
        if(weixiugongdan.getGdId()!=0){
            weixiugongdan.setGdSh(weixiugongdan.getGdSh());
        }else if(weixiugongdan.getGdId() == 0){
            weixiugongdan.setGdSh("待检测");
        }
        Weixiugongdan save = weiXiuGongDanDao.save(weixiugongdan);

       // System.err.println("weixiugongdan.getWxgdxiangqing().size():"+weixiugongdan.getWxgdxiangqing().size());
        Wxgdxiangqing wx = null;
        for (int i=0;i<save.getWxgdxiangqing().size();i++){
            wx = new Wxgdxiangqing();
            wx.setDdcpNum(save.getWxgdxiangqing().get(i).getDdcpNum());
            wx.setDdcpPrice(save.getWxgdxiangqing().get(i).getDdcpPrice());
            wx.setDdxqXiaoji(save.getWxgdxiangqing().get(i).getDdxqXiaoji());
            wx.setWeixiugongdan(save);
            wx.setProductt(save.getWxgdxiangqing().get(i).getProductt());
            wxgdXiangQingDao.save(wx);
        }
    }

    //查看所有维修工单
    public List<Weixiugongdan> selectAllWeiXiugd(String kName,String gdHao,String time,String empName) {
        return weiXiuGongDanMapper.selectAllWeiXiugd(kName,gdHao,time,empName);
    }

    //移除工单
    public void yichuWXGD(Integer gdId) {
        weiXiuGongDanDao.yichuWXGD(gdId);
    }

    //根据联系人id查到联系人信息
    public Contacts ckLianXiRenBycId(Integer cId) {
        return weiXiuGongDanMapper.ckLianXiRenBycId(cId);
    }

    //根据部门id查到部门信息
    public Dept ckBuMenBydeptId(Integer deptId) {
        return weiXiuGongDanMapper.ckBuMenBydeptId(deptId);
    }

    //根据产品id查到产品信息
    public Productt ckCPxx(Integer producttId) {
        return weiXiuGongDanMapper.ckCpxx(producttId);
    }



    //根据维修工单id查看维修检测
    public Wxjiance seleceWxjcByGdId(Integer gdId){

        return wxJianCeMapper.seleceWxjcByGdId(gdId);
    }
    //查看所有的员工
    public List<User> selectAllEmpp(){
        return wxJianCeMapper.selectAllEmpp();
    }

    //新增检测
    public void addJianCe(Wxjiance wxjiance) {
        wxJianCeDao.save(wxjiance);
    }

    //检测完成
    public void jianceWanCheng(Integer gdId) {
        weiXiuGongDanDao.updateGdJingDu("待与客户确认",gdId);
    }

    //根据维修工单id查看客户确定
    public Wxqueding seleceWxqdByGdId(Integer gdId){
        return wxQueDingMapper.seleceWxqdByGdId(gdId);
    }

    //新增客户确定
    public void addkhQueDing(Wxqueding wxqueding) {
        wxQueDingDao.save(wxqueding);
    }

    //根据维修工单id查看客户确定
    public Wxqueding selectKHqueDing(Integer gdId) {
        return wxQueDingMapper.seleceWxqdByGdId(gdId);
    }

    //确定维修quedingWeiXiu
    public void quedingWeiXiu(Integer gdId) {
        weiXiuGongDanDao.updateGdJingDu("待维修",gdId);
    }
    //确定不维修
    public void quedingBuWeiXiu(Integer gdId) {
        weiXiuGongDanDao.updateGdJingDu("待交付",gdId);
        weiXiuGongDanDao.updateGdbuWeixiu(gdId);
    }


    //根据维修工单id查看维修过程
    public Wxguocheng seleceWxgcByGdId(Integer gdId){
        return wxGuoChengMapper.seleceWxgcByGdId(gdId);
    }

    //新增维修过程
    public void addWeiXiuGuoCheng(Wxguocheng wxguocheng) {
        wxGuoChengDao.save(wxguocheng);
    }

    //修改维修工单的进度为维修中
    public void kaiShiWeiXiu(Integer gdId) {
        weiXiuGongDanDao.updateGdJingDu("维修中",gdId);
    }

    //修改维修工单的进度为修理完成
    public void weiXiuWanCheng(Integer gdId) {
        weiXiuGongDanDao.updateGdJingDu("修理完成",gdId);
        wxGuoChengDao.updateweixiuguocheng("已完成",gdId);
    }

    //根据维修工单id查看维修交付
    public Wxjiaofu seleceWxjfByGdId(Integer gdId){
        return wxJiaoFuMapper.seleceWxjfByGdId(gdId);
    }

    //根据工单id查看维修交付
    public Wxjiaofu selectwxJiaoFu(Integer gdId) {
        return wxJiaoFuMapper.seleceWxjfByGdId(gdId);
    }

    //新增维修交付
    public void jiaoFuGeiKeHu(Wxjiaofu wxjiaofu) {
        Date date = new Date();
        wxjiaofu.setWjTime(new Timestamp(date.getTime()));
        wxjiaofu.setWjZhuangtai("已交付");
        wxJiaoFuDao.save(wxjiaofu);
        weiXiuGongDanDao.updateGdJingDu("已交付",wxjiaofu.getGdId());
    }

    //根据维修工单id查看维修工单
    public Weixiugongdan selectWeiXiugdByGdId(Integer gdId){
        return weiXiuGongDanMapper.selectWeiXiugdByGdId(gdId);
    }

    //根据维修工单id查看维修收款
    public List<Wxshoukuan> seleceWxshoukuanByGdId(Integer gdId){
        return wxShouKuanMapper.seleceWxshoukuanByGdId(gdId);
    }

    //新增维修收款
    public void addHuiKuang(Wxshoukuan wxshoukuan) {
        wxShouKuanDao.save(wxshoukuan);
        //如果此维修工单的所有维修收款加起来等于维修应收款
        //应回款金额
        double ygaihmoney = weiXiuGongDanMapper.selectWeiXiugdByGdId(wxshoukuan.getGdId()).getWeixiuMoney().doubleValue();
        //已回金额
        double yjinghmoney = 0;
        //根据维修工单id查看此维修工单的所有维修收款
        List<Wxshoukuan> sk = wxShouKuanMapper.seleceWxshoukuanByGdId(wxshoukuan.getGdId());
        for(int i=0;i<sk.size();i++){
            yjinghmoney = yjinghmoney + sk.get(i).getSkMoney().doubleValue();
        }
        if(ygaihmoney == yjinghmoney){
            weiXiuGongDanDao.updateGdJingDu("已完成",wxshoukuan.getGdId());
            weiXiuGongDanDao.updateGdZhuangTai("结束",wxshoukuan.getGdId());
        }

        //还要修改维修工单的已收款
        double yishouKuang = weiXiuGongDanMapper.selectWeiXiugdByGdId(wxshoukuan.getGdId()).getWeixiuYishoumoney().doubleValue();
        double fk = wxshoukuan.getSkMoney().doubleValue();
        double ysk = yishouKuang + fk;
        weiXiuGongDanDao.updateWXyiShouKuang(new BigDecimal(ysk),wxshoukuan.getGdId());

    }


    //查看待检测维修工单
    public List<Weixiugongdan> selectdaiJianCe(String tj,String empName){
        return weiXiuGongDanMapper.selectdaiJianCe(tj,empName);
    }
    //查看待与客户确认维修工单
    public List<Weixiugongdan> selectdaiYvKeHuQueRen(String tj,String empName){
        return weiXiuGongDanMapper.selectdaiYvKeHuQueRen(tj,empName);
    }
    //查看待维修维修工单
    public List<Weixiugongdan> selectdaiWeiXiu(String tj,String empName){
        return weiXiuGongDanMapper.selectdaiWeiXiu(tj,empName);
    }
    //查看维修中维修工单
    public List<Weixiugongdan> selectweiXiuZhong(String tj,String empName){
        return weiXiuGongDanMapper.selectweiXiuZhong(tj,empName);
    }
    //查看修理完成维修工单
    public List<Weixiugongdan> selectxiuLiWanCheng(String tj,String empName){
        return weiXiuGongDanMapper.selectxiuLiWanCheng(tj,empName);
    }
    //查看已交付维修工单
    public List<Weixiugongdan> selectyiJiaoFu(String tj,String empName){
        return weiXiuGongDanMapper.selectyiJiaoFu(tj,empName);
    }
    //查看已交付未尽收款）维修工单
    public List<Weixiugongdan> selectyiJiaoFu22(String tj,String empName){
        return weiXiuGongDanMapper.selectyiJiaoFu22(tj,empName);
    }
    //查看已完成维修工单
    public List<Weixiugongdan> selectyiWanCheng(String tj,String empName){
        return weiXiuGongDanMapper.selectyiWanCheng(tj,empName);
    }

    //查看待交付维修工单
    public List<Weixiugongdan> selectdaiJiaoFu(String tj,String empName) {
        return weiXiuGongDanMapper.selectdaiJiaoFu(tj,empName);
    }


    //根据工单id查看工单详情
    public List<Wxgdxiangqing> selectgdXQbygdId(Integer gdId){
        return weiXiuGongDanMapper.selectgdXQbygdId(gdId);
    }

    @Autowired
    LogingMapper logingMapper;
    public List<User> weixiuEmp(){
        return logingMapper.weixiuEmp();
    }
    public List<User> jianceEmp(){
        return logingMapper.jianceEmp();
    }
}